//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// J2ME-Lib source file
// Copyright (c) 2006 Elmar Sonnenschein / esoco GmbH
// Last Change: 10.11.2006 by eso
//
// J2ME-Lib is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// J2ME-Lib is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with J2ME-Lib; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA or use the contact
// information from the GNU website http://www.gnu.org
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package de.esoco.j2me.resource;

import java.util.Hashtable;


/********************************************************************
 * Implementation of ResourceBundle that allows easy creation of resource files.
 * Modelled after the JDK {@code java.util.ListResourceBundle} class, but
 * simplified to respect the constrained environment of J2ME devices.
 *
 * @author eso
 */
public abstract class ListResourceBundle extends ResourceBundle
{
	//~ Instance fields --------------------------------------------------------

	private Hashtable aResourceTable;

	//~ Constructors -----------------------------------------------------------

	/***************************************
	 * Constructor for a new ListResourceBundle object. This constructor will
	 * invoke a subclasses' getContents method and fill the internal data
	 * structures with the objects from the returned Object[][] array.
	 *
	 * @see #getContents()
	 */
	public ListResourceBundle()
	{
		Object[][] rContents = getContents();

		aResourceTable = new Hashtable(rContents.length);

		for (int i = 0; i < rContents.length; i++)
		{
			Object[] rLine = rContents[i];

			aResourceTable.put(rLine[0], rLine[1]);
		}
	}

	//~ Methods ----------------------------------------------------------------

	/***************************************
	 * Abstract method through which subclasses return the actual array of
	 * resource objects.
	 *
	 * @return Object[][]
	 */
	protected abstract Object[][] getContents();

	/***************************************
	 * Returns an object from this ResourceBundle.
	 *
	 * @see ResourceBundle#handleGetObject(String)
	 */
	protected Object handleGetObject(String sKey)
	{
		return aResourceTable.get(sKey);
	}
}
